package com.chenjiahao.kaiyanwan.ui.videodetail

import android.app.Activity
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import com.chenjiahao.kaiyanwan.KaiYanApplication
import com.chenjiahao.kaiyanwan.dataFormat
import com.chenjiahao.kaiyanwan.logic.dao.Repository
import com.chenjiahao.kaiyanwan.logic.util.NetworkUtil
import com.chenjiahao.kaiyanwan.showToast
import com.hazz.kotlinmvp.mvp.model.bean.HomeBean

/**
 * Created by chenjh on 2020/9/15
 */
class VideoDetailViewModel : ViewModel() {
    private val idLiveData = MutableLiveData<Long>()

    var mItemList = ArrayList<HomeBean.Issue.Item>()

    lateinit var playUrl: String

    lateinit var mItemData: HomeBean.Issue.Item

    //获取相关视频
    val RelatedData = Transformations.switchMap(idLiveData) {
        Repository.getRelatedData(it)
    }


    fun requestRelatedData() {
        idLiveData.value = mItemData.data?.id?:0
    }

    //加载视频信息
    fun loadVideoInfo(mItemData: HomeBean.Issue.Item) {
        val playInfo = mItemData.data?.playInfo
        val netType = NetworkUtil.isWifi(KaiYanApplication.context)
        if (playInfo!!.size > 1) {
            // 当前网络是 Wifi环境下选择高清的视频
            if (netType) {
                for (i in playInfo) {
                    if (i.type == "high") {
                        playUrl = i.url
                        break
                    }
                }
            } else {
                //否则就选标清的视频
                for (i in playInfo) {
                    if (i.type == "normal") {
                        playUrl = i.url
                        KaiYanApplication.context.showToast(
                            "本次消耗${KaiYanApplication.context
                                .dataFormat(i.urlList[0].size)}流量"
                        )
                        break
                    }
                }
            }
        } else {
            playUrl = mItemData.data.playUrl
        }
        this.mItemData = mItemData
    }

}